package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.ItemdetailsMapper;
import com.example.demo.nuoqi.pojo.Itemdetails;
import com.example.demo.nuoqi.service.ItemdetailsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Itemdetails业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ItemdetailsServiceImpl implements ItemdetailsService {

    @Autowired
    private ItemdetailsMapper itemdetailsMapper;


    /**
     * Itemdetails条件+分页查询
     * @param itemdetails 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Itemdetails> findPage(Itemdetails itemdetails, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(itemdetails);
        //执行搜索
        return new PageInfo<Itemdetails>(itemdetailsMapper.selectByExample(example));
    }

    /**
     * Itemdetails分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Itemdetails> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Itemdetails>(itemdetailsMapper.selectAll());
    }

    /**
     * Itemdetails条件查询
     * @param itemdetails
     * @return
     */
    @Override
    public List<Itemdetails> findList(Itemdetails itemdetails){
        //构建查询条件
        Example example = createExample(itemdetails);
        //根据构建的条件查询数据
        return itemdetailsMapper.selectByExample(example);
    }


    /**
     * Itemdetails构建查询对象
     * @param itemdetails
     * @return
     */
    public Example createExample(Itemdetails itemdetails){
        Example example=new Example(Itemdetails.class);
        Example.Criteria criteria = example.createCriteria();
        if(itemdetails!=null){
            // 
            if(!StringUtils.isEmpty(itemdetails.getId())){
                    criteria.andEqualTo("id",itemdetails.getId());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getItemName())){
                    criteria.andEqualTo("itemName",itemdetails.getItemName());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getItemIntroduction())){
                    criteria.andEqualTo("itemIntroduction",itemdetails.getItemIntroduction());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getTechnicalDirection())){
                    criteria.andEqualTo("technicalDirection",itemdetails.getTechnicalDirection());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getCreatePerson())){
                    criteria.andEqualTo("createPerson",itemdetails.getCreatePerson());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getPrincipal())){
                    criteria.andEqualTo("principal",itemdetails.getPrincipal());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getItemPersonNum())){
                    criteria.andEqualTo("itemPersonNum",itemdetails.getItemPersonNum());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getParticipant())){
                    criteria.andEqualTo("participant",itemdetails.getParticipant());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getItemStatus())){
                    criteria.andEqualTo("itemStatus",itemdetails.getItemStatus());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getTimeCycle())){
                    criteria.andEqualTo("timeCycle",itemdetails.getTimeCycle());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getCreateTime())){
                    criteria.andEqualTo("createTime",itemdetails.getCreateTime());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getEndTime())){
                    criteria.andEqualTo("endTime",itemdetails.getEndTime());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getUserId())){
                    criteria.andEqualTo("userId",itemdetails.getUserId());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getItemClaim())){
                    criteria.andEqualTo("itemClaim",itemdetails.getItemClaim());
            }
            // 
            if(!StringUtils.isEmpty(itemdetails.getProfessionId())){
                    criteria.andEqualTo("professionId",itemdetails.getProfessionId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        itemdetailsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Itemdetails
     * @param itemdetails
     */
    @Override
    public void update(Itemdetails itemdetails){
        itemdetailsMapper.updateByPrimaryKey(itemdetails);
    }

    /**
     * 增加Itemdetails
     * @param itemdetails
     */
    @Override
    public void add(Itemdetails itemdetails){
        // 发布项目
        itemdetails.setItemStatus("1");
        itemdetailsMapper.insert(itemdetails);
    }

    /**
     * 根据ID查询Itemdetails
     * @param id
     * @return
     */
    @Override
    public Itemdetails findById(Integer id){
        return  itemdetailsMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Itemdetails全部数据
     * @return
     */
    @Override
    public List<Itemdetails> findAll() {
        return itemdetailsMapper.selectAll();
    }
}
